package com.ruoyi.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.domain.SysArea;
import com.ruoyi.web.domain.WorkForm;
import com.ruoyi.web.domain.vo.HistogramVo;
import com.ruoyi.web.domain.vo.LargeScreenDataVo;
import com.ruoyi.web.domain.vo.PieChartVo;
import com.ruoyi.web.domain.vo.WorkFormLargeScreenRollVo;
import com.ruoyi.web.service.SysAreaService;
import com.ruoyi.web.service.WorkFormService;
import com.ruoyi.websocket.WebSocketManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/largeScreen")
@Api(tags = "大屏控制层")
public class LargeScreenController extends BaseController {

    @Autowired
    private WorkFormService workFormService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysAreaService sysAreaService;

    @ApiOperation("大屏数据查询")
    @GetMapping("/getData")
    public AjaxResult getData(@RequestParam(value = "province") String province,
                              @RequestParam(value = "city", required = false) String city,
                              @RequestParam(value = "county", required = false) String county){
        LargeScreenDataVo largeScreenDataVo = new LargeScreenDataVo();
        HistogramVo appealType = workFormService.getAppealTypesCount(province, city, county);
        List<PieChartVo> serviceGrade = workFormService.getServiceGradesCount(province, city, county);
        Map<String, Object> counts = workFormService.getCounts(province, city, county);
        SysArea citySysArea = sysAreaService.getOne(new LambdaQueryWrapper<SysArea>().eq(SysArea::getName, city));
        String cityCode = null;
        if (citySysArea != null) {
            cityCode = citySysArea.getCode();
        }
        Long enterpriseTotal = sysUserService.getEnterpriseTotal("370000", cityCode, county);
        List<PieChartVo> serviceState = workFormService.getServiceStatesCount(province, city, county);
        List<PieChartVo> enterpriseSatisfaction = workFormService.getEnterpriseSatisfactionRatio(province, city, county);
        HistogramVo citiesAppealQuantity = workFormService.getCitiesAppealQuantityCount(province, city, county);
        largeScreenDataVo.setAppealType(appealType);
        largeScreenDataVo.setServiceGrade(serviceGrade);
        largeScreenDataVo.setProblemTotal((Long) counts.get("problemTotal"));
//        BigDecimal settledTotal = (BigDecimal) counts.get("settledTotal");
//        largeScreenDataVo.setSettledTotal(settledTotal.longValue());
        Long settledTotal = (Long) counts.get("settledTotal");
        largeScreenDataVo.setSettledTotal(settledTotal);
        BigDecimal settledRate = (BigDecimal) counts.get("settledRate");
        largeScreenDataVo.setSettledRate(settledRate.longValue());
        largeScreenDataVo.setEnterpriseTotal(enterpriseTotal);
        largeScreenDataVo.setServiceState(serviceState);
        largeScreenDataVo.setEnterpriseSatisfaction(enterpriseSatisfaction);
        largeScreenDataVo.setCitiesAppealQuantity(citiesAppealQuantity);
        List<WorkForm> list = workFormService.list(new LambdaQueryWrapper<WorkForm>().eq(WorkForm::getStatus, Byte.parseByte("0")).orderByDesc(WorkForm::getCreateTime).last("LIMIT 1"));
        List<WorkFormLargeScreenRollVo> collect = list.stream().map(i -> {
            WorkFormLargeScreenRollVo workFormLargeScreenRollVo = new WorkFormLargeScreenRollVo();
            workFormLargeScreenRollVo.setEnterpriseName(i.getEnterpriseName());
            workFormLargeScreenRollVo.setProblemDescription(i.getProblemDescription());
            String countyArea = i.getCounty();
            String cityArea = i.getCity();
            if (StringUtils.isNotBlank(cityArea)) {
                countyArea = cityArea + "-" + countyArea;
            }
            workFormLargeScreenRollVo.setArea(countyArea);
            return workFormLargeScreenRollVo;
        }).collect(Collectors.toList());
        largeScreenDataVo.setWorkFormLargeScreenRollList(collect);
        return AjaxResult.success(largeScreenDataVo);
    }
}
